﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Workflow.Alerts.Interfaces.ProfileSettings;
using Sitecore.Data.Items;
using System.Data;

namespace Workflow.Alerts.Sublayouts.ProfileSettings
{
    public partial class ArchiveTime : BaseControl, IArchiveTimeView
    {
        #region IArchiveTimeView Members

        public Int32? Days
        {
            get
            {
                if (txtDays.Value.HasValue)
                    return (int)txtDays.Value.Value;
                return null;
            }
            set
            {
                if (value > 0)
                    txtDays.Text = value.ToString();
            }
        }

        public DataTable ArchiveExportGrid
        {
            set
            {
                rgArchive.DataSource = value;
                rgArchive.DataBind();
            }
        }

        public event OnArchiveTimeViewLoad OnPageLoad;
        public event OnArchiveTimeViewUpdate OnUpdate;
        public event OnArchiveExport OnExport;


        #endregion

        #region Events

        protected void Page_Load(object sender, EventArgs e)
        {
            PresentationManager.RegisterView(typeof(ArchiveTimePresenter), this, new WebSessionProvider());

            if (!IsPostBack && OnPageLoad != null)
            {
                OnPageLoad();
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (OnUpdate != null)
            {
                OnUpdate();
                lblUpdateSuccess.Visible = true;
            }
        }


        protected void btnExportArchive_Click(object sender, EventArgs e)
        {
            if (OnExport != null)
            {
                OnExport();
                rgArchive.ExportSettings.FileName = String.Format("Archive_{0}", DateTime.Now.ToString("yyyyMMdd"));
                rgArchive.MasterTableView.ExportToExcel();
            }
        }

        #endregion
    }
}